/*
 * ************************************************************************
 * 本文件归属于西安协同时光软件有限公司，未经许可，不得擅自使用、拷贝和修改 项目组:金堆城钼业股份有限公司科研项目管理信息系统项目组 版 本: 1.0 日
 * 期：Mar 24, 2010 时 间：10:08:58 AM 作 者： Administrator
 * ************************************************************************
 */
package com.manager.commons;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualTreeBidiMap;
import org.apache.log4j.Logger;

import com.application.department.entity.Department;
import com.application.role.entity.Role;
import com.application.usertable.entity.Usertable;
import com.manager.commons.basemanage.commons.SelectStaticData;
import com.manager.commons.basemanage.entity.BaseDataList;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.config.Constants;
import com.manager.sms.SmsSend;
import com.manager.workflow.WorkFlowConstants;
import com.util.ApplicationUtil;

/**
 * 系统中公共方法
 * 
 * @author Administrator
 * @since jdk1.6
 */
public class CommonUtil {
	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());
	

	/**
	 * 根据配置的规则获得年度下拉框
	 * 
	 * @return
	 */
	public static List getYearSelect() {
		Calendar calendar = Calendar.getInstance();              
		int currentYear = calendar.get(Calendar.YEAR);
		int yearConfigSetUp = Integer.valueOf(ConfigFactory.getInstance().getConfig().getString(Constants.SYC_SYSTEM,Constants.YEAR_SETUP));		
		List list = new ArrayList();
		Map yearMap = new TreeMap();
		for(int i = 0; i < yearConfigSetUp; i++) {
			yearMap.put(String.valueOf(currentYear-(yearConfigSetUp-i)), String.valueOf(currentYear-(yearConfigSetUp-i)));
		}
		yearMap.put(String.valueOf(currentYear), String.valueOf(currentYear));
		for(int i = 0; i < yearConfigSetUp; i++) {
			yearMap.put(String.valueOf(currentYear+(i+1)), String.valueOf(currentYear+(i+1)));
		}
		list.add(yearMap);
		return list;
	}
	/**
	 * 
	* 获得年份下拉框
	*  
	* @return
	 */
	public static List getYear(){
		List list = new ArrayList();
		Map yearMap = new TreeMap();
		int year = 1940;
		for(int i=0;i<80;i++){
			yearMap.put((year+i)+"",(year+i)+"");
		}
		list.add(yearMap);
		return list;
	}	
	/**
	 * 
	* 获得月度下拉框
	*  
	* @return
	 */
	public static List getMonth(){
		List list = new ArrayList();
		Map quarterMap = new TreeMap();
		quarterMap.put("01","01");
		quarterMap.put("02","02");
		quarterMap.put("03","03");
		quarterMap.put("04","04");
		quarterMap.put("05","05");
		quarterMap.put("06","06");
		quarterMap.put("07","07");
		quarterMap.put("08","08");
		quarterMap.put("09","09");
		quarterMap.put("10","10");
		quarterMap.put("11","11");
		quarterMap.put("12","12");
		list.add(quarterMap);
		return list;
	}	
	/**
	 * 
	* 获得季度下拉框
	*  
	* @return
	 */
	public static List getQuarter(){
		List list = new ArrayList();
		Map quarterMap = new TreeMap();
		quarterMap.put("1","一");
		quarterMap.put("2","二");
		quarterMap.put("3","三");
		quarterMap.put("4","四");
		list.add(quarterMap);
		return list;
	}
	
	/**
	 * 
	*季度对应月份
	*  
	* @return
	 */
	public static Map getQuarterMonth(){
		List list=null;
		Map quarterMap = new TreeMap();
		
		
		list=new ArrayList();
		list.add("01");
		list.add("02");
		list.add("03");
		quarterMap.put("1",list);
		list=new ArrayList();
		list.add("04");
		list.add("05");
		list.add("06");
		quarterMap.put("2",list);
		list=new ArrayList();
		list.add("07");
		list.add("08");
		list.add("09");
		quarterMap.put("3",list);
		list=new ArrayList();
		list.add("10");
		list.add("11");
		list.add("12");
		quarterMap.put("4",list);
		return quarterMap;
	}
	
	/**
	 * 
	* TODO 根据月份判断是几季度
	*  
	* @param month
	* @return
	 */
	public static int checkQuarter(int month){
		if(month>0 && month<4)return 1;
		if(month>3 && month<7)return 2;
		if(month>6 && month<10)return 3;
		if(month>9 && month<13)return 4;
		return 0;
	}
	
	
	
	/**
	 * 
	* TODO 根据季度份判断是季度的起始月和结束月
	*  
	* @param month
	* @return
	 */
	public static List getMonth(int quarter){
		List list=new ArrayList();
		if(quarter==1) {list.add("1");list.add("3");}
		if(quarter==2) {list.add("4");list.add("6");}
		if(quarter==3) {list.add("7");list.add("9");}
		if(quarter==4) {list.add("10");list.add("12");}
		return list;
	}
	
	/**
	 * 
	* 获得月度下拉框
	*  
	* @return
	 */
	public static List getMoon(){
		List list = new ArrayList();
		Map quarterMap = new TreeMap();
		quarterMap.put("01","一");
		quarterMap.put("02","二");
		quarterMap.put("03","三");
		quarterMap.put("04","四");
		quarterMap.put("05","五");
		quarterMap.put("06","六");
		quarterMap.put("07","七");
		quarterMap.put("08","八");
		quarterMap.put("09","九");
		quarterMap.put("10","十");
		quarterMap.put("11","十一");
		quarterMap.put("12","十二");
		list.add(quarterMap);
		return list;
	}	
	/**
	 * 
	* 得到当前年份
	*  
	* @return
	 */
	public static String getCurrentYear(){
		Calendar calendar = Calendar.getInstance();              
		int currentYear = calendar.get(Calendar.YEAR);
		return String.valueOf(currentYear);
	}
	
	/**
	 * 
	* 得到当前年份后两位
	*  
	* @return
	 */
	public static String getCurrentYearLastTwoNum(){
		Calendar calendar = Calendar.getInstance();              
		String currentYear = String.valueOf(calendar.get(Calendar.YEAR));
		return currentYear.substring(2);
	}
	
	/**
	 * 获取项目编号前缀
	 * @return
	 */
	public static String getProjectNoPix(String orgid){
		String pix = "";
		ApplicationUtil au = new ApplicationUtil();
		String org = au.getOrg(orgid).getName();
		if("北京研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_BJ;
		}
		if("深圳研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_SZ;
		}
		if("杭州研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_HZ;
		}
		return pix + getCurrentYearLastTwoNum();
	}
	
	/**
	 * 获取机构编码
	 * @return
	 */
	public static String getOrgPix(String org){
		String pix = "";
		if("北京研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_BJ;
		}
		if("深圳研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_SZ;
		}
		if("杭州研发中心".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_HZ;
		}
		if("总部".equals(org)){
			pix = com.manager.commons.Constants.ORG_PIX_ZB;
		}
		return pix;
	}
	
	/**
	 * 
	* 得到当前月份
	*  
	* @return
	 */
	public static String getCurrentMonth(){
		Calendar calendar = Calendar.getInstance();              
		int currentYear = calendar.get(Calendar.MONTH);
		return String.valueOf(currentYear+1);
	}
	
	/**
	 * 
	* 得到当前季度
	*  
	* @return
	 */
	public static String getCurrentQuarter(){
		String currentQuarter = "";
		Calendar calendar = Calendar.getInstance();              
		int currentMonth = calendar.get(Calendar.MONTH) + 1;
		switch(currentMonth){
			case 1:
			case 2:
			case 3:
				currentQuarter = "1";
				break;
			case 4:
			case 5:
			case 6:
				currentQuarter = "2";
				break;
			case 7:
			case 8:
			case 9:
				currentQuarter = "3";
				break;
			case 10:
			case 11:
			case 12:
				currentQuarter = "4";
				break;
				
		}
		return String.valueOf(currentQuarter);
	}
	
	public static BidiMap quarterMap(){
		BidiMap quarterMap = new DualTreeBidiMap();
		quarterMap.put("一","1");
		quarterMap.put("二","2");
		quarterMap.put("三","3");
		quarterMap.put("四","4");
		return quarterMap;
	}
	
	/**
	 * 获取机构集合
	 * 
	 * @return
	 */
	public static List getOrgSelect() {
		ApplicationUtil au = new ApplicationUtil();	
		List<Department> departList = au.getOrgList();
		List list = new ArrayList();
		Map orgMap = new TreeMap();
		if(departList!=null){
			for (Department department : departList) {
				orgMap.put(department.getName(), department.getName());
			}
		}
		list.add(orgMap);
		return list;
	}
	
	/**
	 * 获取机构集合不含总部
	 * 
	 * @return
	 */
	public static List getOrgSelectNoZB() {
		ApplicationUtil au = new ApplicationUtil();	
		List<Department> departList = au.getOrgList();
		List list = new ArrayList();
		Map orgMap = new TreeMap();
		if(departList!=null){
			for (Department department : departList) {
				if(!department.getName().equals("总部")){
					orgMap.put(department.getId(), department.getName());
				}
			}
		}
		list.add(orgMap);
		return list;
	}
	
	
	
	/**
	 * 
	* 日期的间隔天数  
	*  
	* @param startDate
	* @param endDate
	* @return
	 */
	public static Long getDaysBetween(Date startDate, Date endDate) {   
        Calendar fromCalendar = Calendar.getInstance();   
        fromCalendar.setTime(startDate);   
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);   
        fromCalendar.set(Calendar.MINUTE, 0);   
        fromCalendar.set(Calendar.SECOND, 0);   
        fromCalendar.set(Calendar.MILLISECOND, 0);   
        Calendar toCalendar = Calendar.getInstance();   
        toCalendar.setTime(endDate);   
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);   
        toCalendar.set(Calendar.MINUTE, 0);   
        toCalendar.set(Calendar.SECOND, 0);   
        toCalendar.set(Calendar.MILLISECOND, 0);   
        return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);   
    }  
	
	public static String getDictValue(int value,String dictCode){
		List selectList = (List) SelectStaticData.get(dictCode);
        if(selectList == null) return dictCode;
		for (int i = 0;i<selectList.size();i++) {
			//获得对象
			BaseDataList baseDataList = (BaseDataList) selectList.get(i);
			if(String.valueOf(value).equals(baseDataList.getListCode())){
				return baseDataList.getListName();
			}
		}
		return " ";
	}
    
	
	public static Usertable getCurrentUser(HttpServletRequest request){
		Usertable user = null;
		if(request.getSession().getAttribute("loginUser")!=null){
			user = (Usertable) request.getSession().getAttribute("loginUser");
		}
		
		return user;
	}
	
	public static void putMapVar(HttpServletRequest request,Map mapVar){
		boolean flag = false;
		Usertable user = getCurrentUser(request);
		List<Role> roleList = user.getRoleList();
		for (Role role:roleList) {
			if(role.getName().equals(WorkFlowConstants.ROLE_YFJL)){
				flag = true;
				break;
			}
		}
		if(flag){
			mapVar.put("pathFlagRole","two");
			mapVar.put("approveFlag","agree");
		}else{
			mapVar.put("pathFlagRole","one");
		}
	}
	
	public static void putMapVarFEE(HttpServletRequest request,Map mapVar){
		int flag = 0;
		Usertable user = getCurrentUser(request);
		List<Role> roleList = user.getRoleList();
		for (Role role:roleList) {
			if(role.getName().equals(WorkFlowConstants.ROLE_YFJL)){
				flag = 1;
				break;
			}
			if(role.getName().equals(WorkFlowConstants.ROLE_YFZXFZR)){
				flag = 2;
				break;
			}
		}
		if(flag==1){
			mapVar.put("pathFlagRole","two");
			mapVar.put("approveFlag","agree");
		}else if(flag==2){
			mapVar.put("pathFlagRole","three");
			mapVar.put("approveFlag","agree");
		} else{
			mapVar.put("pathFlagRole","one");
		}
	}
	
	/**
	 * 是否为研发经理
	 * @param uid
	 * @return
	 */
	public static boolean isYFJL(String uid){
		boolean flag = false;
		ApplicationUtil au = new ApplicationUtil();
		List<Role> roleList = au.getUserRole(uid);
		for (Role role:roleList) {
			if(role.getName().equals(WorkFlowConstants.ROLE_YFJL)){
				flag = true;
				break;
			}
		}
	    return flag;
	}
	
	/**
	 * 是否为责任中心负责人
	 * @param uid
	 * @return
	 */
	public static boolean isZXFZR(String uid){
		boolean flag = false;
		ApplicationUtil au = new ApplicationUtil();
		List<Role> roleList = au.getUserRole(uid);
		for (Role role:roleList) {
			if(role.getName().equals(WorkFlowConstants.ROLE_YFZXFZR)){
				flag = true;
				break;
			}
		}
	    return flag;
	}
	
	

	
	/**
	 * 系统数据查看权限
	 * 1  研发人员权限            只能看到自己参与的项目信息
	 * 2  研发经理和中心研发负责人  看到本研发中心的项目信息
	 * 3  分管副总、总经理         看到所有信息
	 * 4  超级管理员
	 */
	public static int POWER_VIEW_TYPE_1 = 1;
	public static int POWER_VIEW_TYPE_2 = 2;
	public static int POWER_VIEW_TYPE_3 = 3;
	
	/**
	 * 短信开关 
	 * 0 关闭
	 * 1 开启
	 */
	public static int SMS_OPEN = 0;
    public static SmsSend smsSend;
    /**
     * 附件存放路径
     */
	public static String ATTACHE_FILE = "";
	
	public static StringBuffer filterData(StringBuffer buf,List args){
		int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
		String orgid = CurrentUserUtils.getCurrentUser().getOid();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		if(roleType == CommonUtil.POWER_VIEW_TYPE_2){
			buf.append(" and p.project_org = ? ");
			args.add(orgid);
		}
		if(roleType == CommonUtil.POWER_VIEW_TYPE_1){
			buf.append(" and (t.project_no in (select project_no from yf_project_teams where user_id=?) or t.operateid = ?)");
			args.add(userid);
			args.add(userid);
		}
		return buf;
	}
	
	public static StringBuffer filterDataXMST(StringBuffer buf,List args){
		int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
		String orgid = CurrentUserUtils.getCurrentUser().getOid();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		if(roleType == CommonUtil.POWER_VIEW_TYPE_2){
			buf.append(" and t.project_org = ? ");
			args.add(orgid);
		}
		if(roleType == CommonUtil.POWER_VIEW_TYPE_1){
			buf.append(" and (t.project_no in (select project_no from yf_project_teams where user_id=?) or t.operateid = ?)");
			args.add(userid);
			args.add(userid);
		}
		return buf;
	}
	
	public static StringBuffer filterDataYanShou(StringBuffer buf,List args){
		Usertable user = CurrentUserUtils.getCurrentUser();
		String orgid = CurrentUserUtils.getCurrentUser().getOid();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		if(user.getIsYFJL().equals("true")){
			buf.append(" and p.project_org = ? ");
			args.add(orgid);
		}
		if(user.getIsYFRY().equals("true")){
			buf.append(" and (t.project_no in (select project_no from yf_project_teams where user_id=?) or t.operateid = ?)");
			args.add(userid);
			args.add(userid);
		}
		return buf;
	}
	
	

	public static StringBuffer filterDataZhiPai(StringBuffer buf,List args){
		Usertable user = CurrentUserUtils.getCurrentUser();
		String orgid = CurrentUserUtils.getCurrentUser().getOid();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		if(user.getIsYFJL().equals("true") || user.getIsFZR().equals("true")){
			buf.append(" and (u.oid=? or w.oid=?) ");
			args.add(orgid);
			args.add(orgid);
		}else{
			if(user.getIsFGFZ().equals("true") || user.getIsZJL().equals("true") ){
				
			}else{
				buf.append(" and (t.TASK_BELONG=? or t.OPERATEID=?) ");
				args.add(userid);
				args.add(userid);
			}
		}
		
		return buf;
	}
	
	public static StringBuffer filterDataFee(StringBuffer buf,List args){
		int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
		String org = CurrentUserUtils.getCurrentUser().getOrg();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		if(roleType == CommonUtil.POWER_VIEW_TYPE_2){
			buf.append(" and t.org = ? ");
			args.add(org);
		}
		if(roleType == CommonUtil.POWER_VIEW_TYPE_1){
			buf.append(" and t.operateid = ?");
			args.add(userid);
		}
		return buf;
	}
	
	public static StringBuffer filterDataFeeStatic(StringBuffer buf,List args){
		Usertable user = CurrentUserUtils.getCurrentUser();
		int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
		String org = CurrentUserUtils.getCurrentUser().getOrg();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		
		if(user.getIsYFJL().equals("true") || user.getIsFZR().equals("true") || user.getIsYFZL().equals("true")){
			buf.append(" and t.org = ? ");
			args.add(org);
		}else{
			if(user.getIsFGFZ().equals("true") || user.getIsZJL().equals("true") ){
				
			}else{
				buf.append(" and t.operateid = ?");
				args.add(userid);
			}
		}
		return buf;
	}
	
	public static StringBuffer filterDataThesis(StringBuffer buf,List args){
		Usertable user = CurrentUserUtils.getCurrentUser();
		int roleType = CurrentUserUtils.getCurrentUser().getRoleType();
		String oid = CurrentUserUtils.getCurrentUser().getOid();
		String userid = CurrentUserUtils.getCurrentUser().getId();
		
		if(user.getIsYFJL().equals("true")){
			buf.append(" and (p.project_org = ?  or t.operateid=? or t.status =1)");
			args.add(oid);
			args.add(userid);
		}else{
			buf.append(" and (t.status = 1  or t.operateid=?)");
			args.add(userid);
		}
		return buf;
	}
	
	public static String getKz(){
		Usertable user = CurrentUserUtils.getCurrentUser();
		List<Role>  role = user.getRoleList();
		for(int i=0;i<role.size();i++){
			if(role.get(i).getName().equals("分管副总")){
				return "k";
			}
		}
		return "g";
	}
	public static String getKz1(){
		Usertable user = CurrentUserUtils.getCurrentUser();
		List<Role>  role = user.getRoleList();
		for(int i=0;i<role.size();i++){
			if(role.get(i).getName().equals("研发经理")){
				return "k";
			}
		}
		return "g";
	}

}
